package com.health.viewscontrols.settings;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.health.R;
import com.health.adapter.DrugManagerAdapter;
import com.health.db.FDbManager;
import com.health.tablebean.BD_DD_DRUG;
import com.health.viewscontrols.NovaActivity;
import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.NovaPtActivity;
import com.health.widget.CommItemDecoration;
import java.util.List;

/**
 * author : xiaoxin
 * date   : 2020/7/25 13:59
 * desc   : 药品列表 点击修改  长按删除
 * update :
 */
public class DrugManagerActivity extends NovaPtActivity {

    private static final String TAG = "DrugManagerActivity";

    private DrugManagerAdapter drugManagerAdapter;
    private FDbManager fdbmanager = null;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RecyclerView drugRecyclerView;
    private LinearLayout ll_finish;
    private TextView tv_add_drug;
    private List<BD_DD_DRUG> allInInfoDB;
    private int drugSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drug_manager);
        fdbmanager = NovaApplication.fdbmanger;
        findViews();
        addListeners();

        ll_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tv_add_drug.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(DrugManagerActivity.this,AddDrugActivity.class)
                        .putExtra("size",drugSize));
            }
        });

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                queryDrugListData();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mSwipeRefreshLayout.setRefreshing(true);
        queryDrugListData();
    }

    private void addListeners() {

        drugManagerAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(DrugManagerActivity.this,AddDrugActivity.class)
                        .putExtra("bean",(BD_DD_DRUG)adapter.getData().get(position))
                        .putExtra("size",drugSize));
            }
        });

        drugManagerAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                deleteItemDrug((BD_DD_DRUG)adapter.getData().get(position),position);
                return true;
            }
        });
    }

    @Override
    public void findViews() {

        ll_finish            = findViewById(R.id.ll_finish);
        tv_add_drug          = findViewById(R.id.tv_add_drug);
        mSwipeRefreshLayout  = findViewById(R.id.mSwipeRefreshLayout);
        drugRecyclerView     = findViewById(R.id.drugRecyclerView);
        initAdapter();
    }

    private void initAdapter() {
        drugManagerAdapter = new DrugManagerAdapter(R.layout.item_drug_list,allInInfoDB);
        drugRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        CommItemDecoration divider1 = CommItemDecoration.createVertical(this,
                Color.parseColor("#D2D2D2"),1, 0);
        drugRecyclerView.addItemDecoration(divider1);
        drugRecyclerView.setAdapter(drugManagerAdapter);
    }

    private void queryDrugListData() {
        List<BD_DD_DRUG> allInInfoDB = fdbmanager.findAllInInfoDB(BD_DD_DRUG.class);
        drugSize = allInInfoDB.size();
        drugManagerAdapter.setNewData(allInInfoDB);
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void setListeners() {

    }

    private void deleteItemDrug(final BD_DD_DRUG bdDdDrug,final int position) {
        new AlertDialog.Builder(this).setTitle("确认删除")
                .setMessage("确定删除 (序号" + bdDdDrug.getId() + ") 所选项吗？")
                .setPositiveButton("删除", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        fdbmanager.deleteInInfoDB(bdDdDrug);
                        drugManagerAdapter.remove(position);
                    }
                }).setNegativeButton("取消", null).show();
    }
}
